/**
 * jims
 */
package com.jims.master.modules.reg.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.reg.entity.ClinicForRegist;
import com.jims.master.modules.reg.entity.ClinicSchedule;
import com.jims.master.modules.reg.service.ClinicForRegistService;
import com.jims.master.modules.reg.service.ClinicScheduleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * Controller
 *
 * @author 刘方舟
 * @version 2017-02-27
 */
@Controller
@RequestMapping(value = "${adminPath}/reg/clinicForRegist")
public class ClinicForRegistController extends BaseController {

    @Autowired
    private ClinicScheduleService clinicScheduleService;

    @Autowired
    private ClinicForRegistService clinicForRegistService;

    @ModelAttribute
    public ClinicForRegist get(@RequestParam(required = false) String id) {
        ClinicForRegist entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = clinicForRegistService.get(id);
        }
        if (entity == null) {
            entity = new ClinicForRegist();
        }
        return entity;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = {"index", ""})
    public String list(ClinicForRegist clinicForRegist, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ClinicForRegist> page = clinicForRegistService.findPage(new Page<ClinicForRegist>(request, response), clinicForRegist);
        model.addAttribute("page", page);
        return "modules/reg/clinicForRegistIndex";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "form")
    public String form(ClinicForRegist clinicForRegist, Model model) {
        Date date = clinicForRegist.getClinicDate();
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
        if (date != null) {
            clinicForRegist.setClinicDateStr(dateformat.format(date));
        }

        model.addAttribute("clinicForRegist", clinicForRegist);
        return "modules/reg/clinicForRegistForm";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "save")
    @ResponseBody
    public StringData save(ClinicForRegist clinicForRegist, Model model) {
        StringData data = new StringData();
        int i = clinicForRegistService.save(clinicForRegist);
        if (i > 0) {
            data.setCode("success");
            data.setData("保存号表调整成功");
        } else {
            data.setCode("error");
            data.setData("保存号表调整失败");
        }
        return data;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "delete")
    @ResponseBody
    public StringData delete(ClinicForRegist clinicForRegist) {
        StringData data = new StringData();
        Map<String, Object> map = clinicForRegistService.delete(clinicForRegist.getClinicForRegistIds());
        Boolean result = (Boolean) map.get("result");
        if (result) {
            data.setCode("success");
            data.setData((String) map.get("info"));
        } else {
            data.setCode("error");
            data.setData((String) map.get("info"));
        }
        return data;
    }

    //-------------------------------以下号表生成相关--chenxy-----------------------------------//


    @RequiresPermissions("user")
    @RequestMapping(value = {"registClinicIndex", ""})
    public String registClinicIndex(ClinicSchedule clinicSchedule, HttpServletRequest request, HttpServletResponse response, Model model) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        clinicSchedule.updateOrgId();
        Page<ClinicSchedule> clinicSchedulePage = clinicScheduleService.findPage(new Page<ClinicSchedule>(request, response), clinicSchedule);
        model.addAttribute("clinicSchedulePage", clinicSchedulePage);
        return "modules/reg/clinicIndexLeft";
    }

    /**
     * 跳转到生成号表页面
     *
     * @param clinicSchedule
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"createRegist", ""})
    public String registPage(ClinicSchedule clinicSchedule, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ClinicSchedule> clinicSchedulePage = clinicScheduleService.findPage(new Page<ClinicSchedule>(request, response), clinicSchedule);
        clinicSchedulePage.setFuncName("clinicSchedulePage");
        model.addAttribute("clinicSchedulePage", clinicSchedulePage);
        ClinicForRegist clinicForRegist = new ClinicForRegist();
        Page<ClinicForRegist> clinicForRegistPage = this.clinicForRegistService.findPage(new Page<ClinicForRegist>(request, response), clinicForRegist);
        model.addAttribute("clinicForRegistPage", clinicForRegistPage);
        return "modules/reg/createRegist";
    }

    /**
     * 查询号表列表
     *
     * @param clinicForRegist
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"registList", ""})
    public String registList(ClinicForRegist clinicForRegist, HttpServletRequest request, HttpServletResponse response, Model model) {
        clinicForRegist.updateOrgId();
        Page<ClinicForRegist> clinicForRegistPage = this.clinicForRegistService.findPage(new Page<ClinicForRegist>(request, response), clinicForRegist);
        model.addAttribute("clinicForRegistPage", clinicForRegistPage);
        return "modules/reg/registList";
    }


    /**
     * chenxy
     * 生成号表
     *
     * @param clinicForRegist
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"regist", ""})
    @ResponseBody
    public StringData regist(ClinicForRegist clinicForRegist, HttpServletRequest request, HttpServletResponse response, Model model) {
        StringData data = new StringData();
        String[] clinicScheduleIdsArr = clinicForRegist.getClinicSchedulesIds().split(",");
        String startTime = clinicForRegist.getStartTime();
        String endTime = clinicForRegist.getEndTime();
        int i = this.clinicForRegistService.regist(clinicScheduleIdsArr, startTime, endTime);
        if (i > 0) {
            data.setCode("success");
            data.setData("号表生成成功");
        } else {
            data.setCode("error");
            data.setData("号表生成失败");
        }
        return data;
    }


}